unit fmMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, DB, Grids, DBGrids, ComCtrls;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    btnGetData: TButton;
    pc1: TPageControl;
    tsLog: TTabSheet;
    tsDataGrid: TTabSheet;
    Memo1: TMemo;
    grd1: TDBGrid;
    dc1: TDataSource;
    edtServerHost: TEdit;
    edtServerPort: TEdit;
    procedure btnGetDataClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  slsqlapi;

//var ds: TslSQLDataset;

procedure TForm1.btnGetDataClick(Sender: TObject);
var
  OutData, SQL, FieldValue, RowStr, SemiCol: string;
  Res, ColumnCount, RecCount: Integer;
  slCSV: TStringList;
  i, RowLen: Integer;
begin
  Memo1.Clear;
  Memo1.Lines.Add('Results of: ');

  SetLength(OutData, BUFFER_LENGTH);
  Res := slConnect('localhost', 51234, 'E0', OutData);
  Memo1.Lines.Add('sluConnect -> Result: ' + IntToStr(Res) + ' OutData: ' + OutData);
  Memo1.Lines.Add('slGetError -> Error: ' + slGetError());

  Res := slOpen('sample.db3', 'C');
  Memo1.Lines.Add('sluOpen -> Result: ' + IntToStr(Res));
  Memo1.Lines.Add('slGetError -> Error: ' + slGetError());

  SQL := 'SELECT * FROM parts LIMIT 10 OFFSET 0';
  Res := slSel(SQL);
  Memo1.Lines.Add('sluSel -> Result: ' + IntToStr(Res));
  Memo1.Lines.Add('slGetError -> Error: ' + slGetError());

  slCSV := TStringList.Create;
  Memo1.Lines.Add(#13#10'Print data:'#13#10);
  RecCount := 0;
  ColumnCount := slGetColumnCount();
  while slGetRow() <> 0 do begin
    Inc(RecCount);
    RowStr := '';
    for i := 1 to ColumnCount do begin
      RowStr := RowStr + '"' + slF(i) + '";';
    end;

    RowLen := Length(RowStr);
    SemiCol := Copy(RowStr, RowLen, 1);
    if SemiCol = ';' then
      Delete(RowStr, RowLen, 1);

    slCSV.Add(RowStr);
    Memo1.Lines.Add(Format('%5d'#9,[RecCount]) + RowStr);
    Application.ProcessMessages;
  end;
//  sluClose();
  Memo1.Lines.Add('sluGetRow: Count: ' + IntToStr(RecCount));

  slCSV.SaveToFile('test.csv');
  FreeAndNil(slCSV);

//  ds := TslSQLDataset.Create(nil);
//  dc1.DataSet := ds;
//  ds.Open;
//  FreeAndNil(ds);
end;

end.
